<script setup>
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelSelect from './component/ChannelSelect.vue'
import ArticleEdit from './component/ArticleEdit.vue'
import { artGetListService, artDelService } from '@/api/article.js'
import { formatTime } from '@/utils/format.js'
const formModel = ref([]) // 表单数据
const total = ref(0) // 总条数
const loading = ref(false) // loading
// 定义参数对象
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})

const svg = `<path class="path" d="
          M 30 15
          L 28 17
          M 25.61 25.61
          A 15 15, 0, 0, 1, 15 30
          A 15 15, 0, 1, 1, 27.99 7.5
          L 15 15
        " style="stroke-width: 4px; fill: rgba(0, 0, 0, 0)"/>`

const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  formModel.value = res.data.data
  total.value = res.data.total
  loading.value = false
}
getArticleList()

// 分页
const onSizeChange = (size) => {
  // 页的大小改变了，留在当前页没有意义，=> 可能当前页不存在， 数据也会发生改变
  // 需要重新渲染第一页
  params.value.pagenum = 1
  params.value.pagesize = size
  getArticleList()
}

const onCurrentChange = (page) => {
  params.value.pagenum = page
  getArticleList()
}

// 搜索 => 根据新的条件重新渲染, 页码重置为1
const onSearch = () => {
  params.value.pagenum = 1
  getArticleList()
}

// 重置 => 重置搜索框的数据
const onReset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}
// 获取抽屉组件dom
const articleEditRef = ref()

const onSuccess = (type) => {
  if (type === 'add') {
    // 添加操作
    // 展示添加的数据 => 最后一页的数据
    params.value.pagenum = Math.ceil((total.value + 1) / params.value.pagesize)
  }
  // 重新渲染数据
  getArticleList()
}
// 发布文章
const onAddArticle = () => {
  articleEditRef.value.open({})
}
const onEditArticle = (row) => {
  articleEditRef.value.open(row)
}

const onDelArticle = async (row) => {
  // 删除提示
  await ElMessageBox.confirm('你确定要删除么', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDelService(row.id)
  ElMessage.success('删除成功')
  getArticleList()
}
</script>
<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle">发布文章</el-button>
    </template>
    <!-- 主体分为两部分 表单 表格 -->
    <el-form inline>
      <el-form-item label="文章分类: ">
        <!-- vue2： v-model => :value 和 @input 的缩写 -->
        <!-- vue3: v-model => :modelValue 和 @update:modelValue 的缩写 -->
        <channel-select v-model:modelValue="params.cate_id"></channel-select>
      </el-form-item>
      <el-form-item label="发布状态: ">
        <el-select v-model="params.state" style="width: 200px">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="onSearch" type="primary">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      element-loading-text="拼命加载中..."
      :element-loading-svg="svg"
      class="custom-loading-svg"
      element-loading-svg-view-box="-10, -10, 50, 50"
      :data="formModel"
      style="width: 100%"
    >
      <el-table-column label="文章标题" width="400" prop="title">
        <template #default="{ row }">
          <el-link type="primary" :underline="false">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate_name"></el-table-column>
      <el-table-column label="发表时间" prop="pub_date">
        <template #default="{ row }">{{ formatTime(row.pub_date) }}</template>
      </el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            circle
            plain
            type="primary"
            :icon="Edit"
            @click="onEditArticle(row)"
          ></el-button>
          <el-button
            circle
            plain
            type="danger"
            :icon="Delete"
            @click="onDelArticle(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[2, 3, 4, 5, 10]"
      :background="true"
      layout="jumper, total, sizes, prev, pager, next"
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin: 20px; justify-content: flex-end"
    />

    <article-edit ref="articleEditRef" @success="onSuccess"></article-edit>
  </page-container>
</template>
